package com.itfenghuang.day06;

import java.sql.*;
import java.util.Scanner;

public class A04_atm {
    public static Scanner sc = new Scanner(System.in);
    public static A05_atm jdbc=new A05_atm();
public static void main(String[] args) throws SQLException, ClassNotFoundException {

        System.out.println("sssssssssss=================================sssssssssss");
        System.out.println("sssssssssss============花Q界面==============sssssssssss");
        System.out.println("sssssssssss=================================sssssssssss");

        System.out.println("1.登陆 2.注册 3.忘记密码 4.挂失 5.解锁 6.退出");
        System.out.println("请选择办理事项");
        String i = sc.nextLine();


        if ("1".equals(i)){
            String name=login();
                if (!("0".equals(name))){
                System.out.println("登陆成功,进入2级界面");
                while(true){
                    System.out.println("1.查询余额 2.存钱 3.取钱 4.转账 5.修改密码 6.查询个人信息 7.返回上一级 8.退出系统");
                    System.out.println("请选择操作");
                    String j=sc.nextLine();
                    if ("1".equals(j)){
                        System.out.println("查询余额");
                        showMoney(name);

                    }else if("2".equals(j)){
                        System.out.println("存钱");
                        saveMoney(name);

                    }else if("3".equals(j)){
                        System.out.println("取钱");
                        getMoney(name);

                    }else if("4".equals(j)){
                        System.out.println("转账");
                    }else if("5".equals(j)){
                        System.out.println("修改密码");
                    }else if("6".equals(j)){
                        System.out.println("查询个人信息");
                    }else if("7".equals(j)){
                        System.out.println("返回上一级");
                    }else if("8".equals(j)){
                        System.out.println("退出系统");
                    }else{
                        System.out.println("没钱看个屁");
                    }
                }
            }
        }
        if ("2".equals(i)) {
            register();
        }


}
//登陆
    public static String login() throws SQLException, ClassNotFoundException {
        while (true) {
            System.out.println("开始登陆,请输入用户名");
            String name = sc.nextLine();

            System.out.println("请输入密码");
            String password = sc.nextLine();
//          1.  查询数据库 根据用户名查密码
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/atm",
                    "root", "123456");
            Statement stat = con.createStatement();

            String sql = "select password  from  atm  where name='" + name + " '";
            ResultSet result = stat.executeQuery(sql);

            if (result.next()) {
                System.out.println("用户名存在,开始比较密码");
                String password1 = result.getString("password");
                System.out.println(password1 + " : " + password);

                con.close();
                stat.close();
                result.close();

                if (password.equals(password1)) {
                    System.out.println("密码正确,登陆成功");
                    return name;
                } else {
                    System.out.println("密码错误请重新输入");
                }

            } else {
                System.out.println("用户名不存在,请从新输入");
            }

        }
    }

//注册
    public static void  register() throws SQLException, ClassNotFoundException {
        while(true){
            System.out.println("开始注册,请输入姓名");
            String name = sc.nextLine();

            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/atm",
                    "root", "123456");
            Statement stat = con.createStatement();
            String sql = "select name  from  atm  where name='" + name + " '";
            ResultSet result = stat.executeQuery(sql);
            if(result.next()){
                System.out.println("姓名已存在");
            }else{
                System.out.println("请输入用户名");
                int user = sc.nextInt();
                System.out.println("请输入密码");
                int password=sc.nextInt();
                String sql1="insert into atm(name,user,password) values('"+name +"','"+user +"','"+password +"')";
                stat.executeUpdate(sql1);
                System.out.println("注册成功");
                break;
            }
        }

    }

//查询余额
    public static void showMoney(String name) throws SQLException, ClassNotFoundException {
        System.out.println("开始查询");
        String sql = "select money from atm where name='"+name+"'";

        A05_atm a05_atm = new A05_atm();
        ResultSet result = a05_atm.select(sql);

        if(result.next()){
            System.out.println(result.getInt("money"));
        }
        System.out.println("查询成功");
    }

//存钱
    public static void saveMoney(String name) throws SQLException, ClassNotFoundException {
            System.out.println("开始存钱,请输入存储金额");
            int money = sc.nextInt();

        String sql = "update  atm  set  money=money + "+money+" where name='" + name + "'";
        A05_atm a05_atm = new A05_atm();
        int result = a05_atm.update(sql);
        System.out.println("存储成功");
    }

// 取钱
    public static void  getMoney(String name) throws SQLException, ClassNotFoundException {
        System.out.println("开始取钱");
        int money = sc.nextInt();

        String sql = "update  atm  set  money=money - "+money+" where name='" + name + "'";
        A05_atm a05_atm = new A05_atm();
        int result = a05_atm.update(sql);
        System.out.println("取钱成功");
    }


// 转账
    public static void changeMoney() throws SQLException, ClassNotFoundException {
        System.out.println("开始转账");
        String s = sc.nextLine();

        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/atm",
                "root", "123456");
        Statement stat = con.createStatement();



    }








}
